package reservation.flight;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import reservation.flight.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ConfirmationActivity extends Activity {
	String origen, destino, salida, regreso, usuario;
	public static final int DIALOGO_CONFIRMACION=1;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.confirmation_reservation);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title_bar);
        Button b_regresar=(Button)findViewById(R.id.CR_Back_B);
        Button b_confimar=(Button)findViewById(R.id.CR_Confirmation_B);
        TextView mod_origen=(TextView)findViewById(R.id.CR_Depart_From_ET);
        TextView mod_destino=(TextView)findViewById(R.id.CR_Arrive_In_ET);
        TextView mod_salida=(TextView)findViewById(R.id.CR_Depart_Date_ET);
        TextView mod_regreso=(TextView)findViewById(R.id.CR_Return_Date_ET);

        Bundle bu= getIntent().getExtras();
        usuario=bu.getString("ID");
        destino=bu.getString("DESTINO");
        origen=bu.getString("ORIGEN");
        salida=bu.getString("SALIDA");
        regreso=bu.getString("REGRESO");
        mod_origen.setText(origen);
        mod_destino.setText(destino);
        mod_salida.setText(salida);
        mod_regreso.setText(regreso);
        
        
        b_regresar.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();				
			}
		});
        b_confimar.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				if(guardarReservacion()){
					Log.i("log_tag", "Se guardo la resevacion");
					onCreateDialog(1);
				}
				// TODO Auto-generated method stub
				
			}
		});
        
	}
	public boolean guardarReservacion(){
		Function fun=new Function();
		
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("opcion","registro"));
        nameValuePairs.add(new BasicNameValuePair("origen",origen));
        nameValuePairs.add(new BasicNameValuePair("destino",destino));
        nameValuePairs.add(new BasicNameValuePair("salida",salida));
        nameValuePairs.add(new BasicNameValuePair("regreso",regreso));
        nameValuePairs.add(new BasicNameValuePair("usuario",usuario));
        String result= fun.conexion(nameValuePairs);
	    return  fun.guardarReservacion(result);
	}
	protected Dialog onCreateDialog(int id) {
	   	 Dialog dialogo = null;
	       switch (id) {
	       case DIALOGO_CONFIRMACION:
	       	new AlertDialog.Builder( this )
	       	.setMessage( getString(R.string.reservation_made) )
	       	.setPositiveButton( "OK", new DialogInterface.OnClickListener() {
	       		public void onClick(DialogInterface dialog, int which) {
	       			Log.d( "AlertDialog", "Positive" );
	       			finish();
	       		}
	       	})        	
	       	.show();
	       	break;
	       }
			return dialogo;
	   }
}
